﻿using MVC_basic.Data_Layer.Interface;
using MVC_basic.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AppsModel.Data_Layer.Concrete
{
    public class dbUser : IdbUser
    {
        public bool createUser(string username, string password, string email, string picUrl)
        {
            throw new NotImplementedException();
        }

        public MVC_basic.Models.User getUser(int id)
        {
            SqlConnection conn = null;
            SqlDataReader rdr = null;
            User user = null;
            try
            {
                conn = Helper.createConnection();

                // 1. create a command object identifying
                // the stored procedure
                SqlCommand cmd = new SqlCommand("getUser", conn);

                // 2. set the command object so it knows
                // to execute a stored procedure
                cmd.CommandType = CommandType.StoredProcedure;

                // 3. add parameter to command, which
                // will be passed to the stored procedure
                cmd.Parameters.Add(new SqlParameter("@id", id));

                // execute the command
                rdr = cmd.ExecuteReader();

                // iterate through results, printing each to console
                while (rdr.Read())
                {
                    user = new User
                    {
                        id = rdr.GetInt32(0), 
                        username = rdr.GetString(1), 
                        email = rdr.GetString(2),
                        picUrl = rdr.GetString(3)
                    };
                }
            }
            finally
            {
                if (conn != null)
                    conn.Close();
                if (rdr != null)
                    rdr.Close();
            }
            return user;
        }

        public void updateUser(int id, string username, string email, string picUrl)
        {
            SqlConnection conn = null;

            try
            {
                conn = Helper.createConnection();

                // 1. create a command object identifying
                // the stored procedure
                SqlCommand cmd = new SqlCommand("updateApp", conn);

                // 2. set the command object so it knows
                // to execute a stored procedure
                cmd.CommandType = CommandType.StoredProcedure;

                // 3. add parameter to command, which
                // will be passed to the stored procedure
                cmd.Parameters.Add(new SqlParameter("@id", id));
                cmd.Parameters.Add(new SqlParameter("@Name", username));
                cmd.Parameters.Add(new SqlParameter("@Email", email));
                cmd.Parameters.Add(new SqlParameter("@PicUrl", picUrl));

                // execute the command
                cmd.ExecuteNonQuery();

            }
            finally
            {
                if (conn != null)
                    conn.Close();
            }	
        }
    }
}
